﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace HighFantasyRPG
{

     
    public class Panel
    {

        private Texture2D panelarea, buttonleft, buttonright;
        private SpriteFont font;
        private ContentManager content;
        public Rectangle area, left, right;
        public Vector2 pos;
        public String panelselection;
        public int selectionid;
        public bool isVisible, buttonpushed;
        public PlayerData playerdata;
        public Texture2D playerface;
        private MouseState mouse;
        private KeyboardState currentKey, OldKey;
        private Input input;
        public char chrs = '1';

        public Panel(ContentManager content)
        {
            this.content = content;
            area = new Rectangle(640, 0, 156, 600);
            pos = new Vector2(640, 0);
            panelarea = content.Load<Texture2D>("Background/Panel");
            buttonleft = content.Load<Texture2D>("Background/options");
            buttonright = content.Load<Texture2D>("Background/profile");
            font = content.Load<SpriteFont>("Fonts/Panel");

            left = new Rectangle(664, 400, 31, 31);
            right = new Rectangle(744, 400, 31, 31);

            input = new Input();
        }

        public void Update(GameTime gameTime, Level level)
        {
            mouse = Mouse.GetState();
            currentKey = Keyboard.GetState();
            

            input.ConvertKeyboardInput(currentKey, OldKey, out chrs);

            if (level.player != null)
                playerdata = level.player.data;
                playerface = level.player.Face;

            if (mouse.LeftButton == ButtonState.Pressed)

                {
            if (left.Contains(new Point(mouse.X, mouse.Y)))

                {
                buttonpushed = true;
                Network.Download("3.xml");
                Network.UploadFile(level.levelIndex.ToString() + ".xml");
                }
            else if (right.Contains(new Point(mouse.X, mouse.Y)))
                {
                buttonpushed = true;
                Network.CreateSession();
                }
                
            else
                {
                buttonpushed = false;

                }

                }

            if (Network.SessionStarted == true)
                {
                    Network.WaitForStartingInfo();
                    Network.CheckServerMessages();
                }
        }

        public void Draw(SpriteBatch spriteBatch, float CameraPosX)
        {
            
            spriteBatch.Draw(panelarea, new Vector2(pos.X + CameraPosX, pos.Y), Color.White);
            spriteBatch.Draw(buttonleft, new Vector2(664 + CameraPosX, 400), Color.White);
            spriteBatch.Draw(buttonright, new Vector2(744 + CameraPosX, 400), Color.White);
            spriteBatch.Draw(playerface, new Vector2(664 + CameraPosX, 28), Color.White);
            spriteBatch.DrawString(font, playerdata.Profile.Name, new Vector2(732 + CameraPosX, 20), Color.White);
            spriteBatch.DrawString(font, "Lvl= " + playerdata.Profile.Level, new Vector2(732 + CameraPosX, 60), Color.White);
            spriteBatch.DrawString(font, playerdata.Profile.Profession, new Vector2(664 + CameraPosX, 100), Color.White);

            if (input != null)
                spriteBatch.DrawString(font, chrs.ToString(), new Vector2(664 + CameraPosX, 270), Color.White);


            if (buttonpushed == true)
                spriteBatch.DrawString(font, "Trying to connect...", new Vector2(664 + CameraPosX, 230), Color.White);

            if (Network.SessionStarted == true)
                {
                    spriteBatch.DrawString(font, Network.message, new Vector2(664 + CameraPosX, 250), Color.White);
                    spriteBatch.DrawString(font, Network.debug, new Vector2(664 + CameraPosX, 270), Color.White);
                }
        }
    }
}
